function [ arrayResultBinary, arrayResultOrg, arrayResultPositionOut, color_binary, Iedge] = MeasureFilter(binaryI3,Iedge, ...
    orginalIma,  IS_DEBUG, conFilter1 ,FOLDER_RESULT, name, result, resultF, arrayResultPositionIn)


%disp('=======DO FILTER=========');
[Ro,Co] = size(binaryI3);

%% connected component analysic
f1 = binaryI3;
cc = bwconncomp(f1, 4);
%View All Objects with visualize
labeled = labelmatrix(cc);
color_binary = label2rgb(labeled, @spring, 'c', 'shuffle'); 
% color_binary = [];

%% extract feature from binary image
featureData = regionprops(cc,'BoundingBox','Orientation');

for i=1:cc.NumObjects    
    
    aspectRatio = featureData(i).BoundingBox(3)/featureData(i).BoundingBox(4);
    oriented = featureData(i).Orientation;
    area = featureData(i).BoundingBox(3)*featureData(i).BoundingBox(4);
%     ratioSizeH = featureData(i).BoundingBox(3)/Co;
%     ratioSizeW = featureData(i).BoundingBox(4)/Ro;
%     a = length(cc.PixelIdxList{i});
%     ratioObjectArea = length(cc.PixelIdxList{i})/area;
    
    if ( aspectRatio>conFilter1(1) && aspectRatio<7 && area> 500 && abs(oriented)<35 )
        grain = false(size(binaryI3));
        grain(cc.PixelIdxList{i}) = true;
%         Imark = orginalIma.*uint8(grain); 
        subImage = imcrop(grain, featureData(i).BoundingBox);
        subImageOrg = imcrop(orginalIma, featureData(i).BoundingBox);
        %         %DEBUG
        
        %         if (ratioObjectArea<0.3 || ratioObjectArea>0.75)
        % %            FindAgainAlgorithm(subImage,imcrop(orginalIma, featureData(i).BoundingBox), strcat(FOLDER_RESULT,'\',name), num2str(i));
        %         else
        
        if aspectRatio < 0.9 || aspectRatio >1.5
            temp = imrotate(subImage, oriented*(-1));
            tempOrg = imrotate(subImageOrg, oriented*(-1));
        else
            temp = subImage;
            tempOrg = subImageOrg;
        end
        
        ccTemp = bwconncomp(temp, 8);
        grainTemp = false(size(temp));
        featureDataTemp = regionprops(ccTemp,'BoundingBox');
        grainTemp(ccTemp.PixelIdxList{1}) = true;
        
        
        aspectRatioTemp = featureDataTemp(1).BoundingBox(3)/featureDataTemp(1).BoundingBox(4);
        areaTemp = featureDataTemp(1).BoundingBox(3)*featureDataTemp(1).BoundingBox(4);
        ratioObjectAreaTemp = length(ccTemp.PixelIdxList{1})/areaTemp;
        ratioSizeHTemp = featureDataTemp(1).BoundingBox(3)/Co;
        ratioSizeWTemp = featureDataTemp(1).BoundingBox(4)/Ro;
        
        
        if (IS_DEBUG)
            imwrite(grainTemp,strcat(FOLDER_RESULT,'\',name,'\_1Meas_',num2str(i),'-',num2str(length(result)),'-as-',num2str(aspectRatioTemp),'-ra-',num2str(ratioObjectAreaTemp),...
                '-raH-',num2str(ratioSizeHTemp),'-raW-',num2str(ratioSizeWTemp),'.JPG'),'JPG');
        end
        %%% Kiem tra anh sau khi xoay xem ty le kich thuoc cua no nhu the
        %%% nao
        if (ratioObjectAreaTemp<0.3 || ratioObjectAreaTemp>0.75|| ratioSizeHTemp > 0.7 || ratioSizeWTemp > 0.8)
            %%%% khong thoa dieu kien thi khong lam gi, tien hanh imdilate
            %%%% lai lan 2
        else
            if (aspectRatioTemp>0.3 && aspectRatioTemp<6.5)                
                if (IS_DEBUG)
                    imwrite(grainTemp,strcat(FOLDER_RESULT,'\',name,'\_1ZZZMeas_',name,num2str(i),'-',num2str(length(result)),'.JPG'),'JPG');
                end
                %%% tien hanh gan lai cac pixcel cua nhung object da lay ra
                %%% duoc
                idex = length(result)+1;
                Iedge (cc.PixelIdxList{i}) = true;
                result{idex} = imcrop(grainTemp, featureDataTemp(1).BoundingBox);
                resultF{idex} = imcrop(tempOrg, featureDataTemp(1).BoundingBox);
                arrayResultPositionIn{idex} = featureData(i).BoundingBox;
            end
        end
        
        %         end
    end
end
arrayResultBinary = result;
arrayResultOrg = resultF;
arrayResultPositionOut = arrayResultPositionIn;
end


